<?php
namespace Game\Controller;
use Common\Controller\AdminbaseController;
use Library\GameTable;
class MachineController extends AdminbaseController {
	protected $device_model,$game_machine_modell,$region_model;
	function _initialize() {
		parent::_initialize();
		$this->device_model = D("Device");
		$this->game_machine_model = D("GameMachine");
		$this->region_model = D("Region");
		$this->store_model = D("Store");
	}

    private function devicestatus() {
		$devicestatus =
		array(
			array(
	            array('id' => '0','name'=>'请选择'),
	            array('id' => '1','type'=>'AD','name'=>'液晶屏设备'),
	            array('id' => '2','type'=>'TA','name'=>'游戏娱乐桌'),
	        ),
	        array(
	            array('id' => '0', 'name'=>'请选择'),
	            array('id' => '1', 'name'=>'条形码'),
	            array('id' => '2', 'name'=>'设备ID'),
	            array('id' => '3', 'name'=>'美冠ID'),
	        ),
		);
		return $devicestatus;
    }
    function index(){
//			$device['barcode'] = 'TA4303887713';
//			$tmlsns = GameTable::getTerminalsByTmlSNs(array($device['barcode']));
//			$device['tmlid'] = '33218216';
//			$tmlsns = GameTable::getTerminalStateByIds(array($device['tmlid']));
//			if(is_array($tmlsns) && count($tmlsns)){
//				dump($tmlsns);
//			}
		$bstatus = $this->devicestatus();
		if(I('param.')){
			$keyword =I('param.keyword');
			if(I('param.search') ==1){$search[] = array('tmlsn'=>array('like',"%$keyword%"));}
			if(I('param.search') ==2){$search[] = array('devid'=>array('like',"%$keyword%"));}
			if(I('param.search') ==3){$search[] = array('tmlid'=>array('like',"%$keyword%"));}
			$snstype = $bstatus[0];
            if(I('param.snstype')){
            	$sn =$snstype[I('param.snstype')]['type'];
                $search[] = array('tmlsn'=>array('like',"$sn%"));
            }
            if(I('param.online')){
            	$online =I('param.online');
                $search[] = array('online'=>$online);
            }
            if(I('param.start_time')){
                    $search[] = array('tmltime'=>array('egt',I('param.start_time')));
            }
            if(I('param.end_time')){
                    $search[] = array('tmltime'=>array('elt',I('param.end_time')));
            }
            $_GET['online'] = I('param.online');
            $_GET['snstype'] = I('param.snstype');
			$_GET['keyword']=I('param.keyword');
            $_GET['start_time'] = I('param.start_time');
            $_GET['end_time'] = I('param.end_time');
		}
		$count=$this->game_machine_model->where($search)->count();
		$page = $this->page($count, 50);
		$machine=$this->game_machine_model
		->where($search)
		->limit($page->firstRow . ',' . $page->listRows)
		->order("id DESC")
		->select();
		if($machine){
			foreach($machine as $mac){
				$store_id[] = $mac['store_id'];
			}
			$stores=$this->store_model->where(array('store_id'=>array('in',$store_id)))->select();
			$this->assign("stores", $stores);
		}
		$this->assign("snstype", $bstatus[0]);
		$this->assign("search", $bstatus[1]);
    	$this->assign('page', $page->show('Admin'));
		$this->assign("machine", $machine);
		$this->assign("count",$count);
		$this->display();
	}
	//机器状态/详细信息查
	function edit(){
		$machine = $this->game_machine_model->where(array('id'=>I("get.id")))->find();
		if($machine['tmlsn']){
			$tmlsns = GameTable::getTerminalsByTmlSNs(array($machine['tmlsn']));
			if(is_array($tmlsns)){
				$this->assign("tmlsns",json_decode($tmlsns[0]['tmlVolumeJson'], true));
				$this->assign("bright",json_decode($tmlsns[0]['tmlBrightness'], true));
			}
		}else{
			$machine = array('id' => '0', 'error'=>'获取失败');
		}
		if($machine['store_id']){
			$store = $this->store_model->where(array('store_id'=>$machine['store_id']))->find();
			$this->assign("store", $store);
		}
		$this->assign("machine",$machine);
		$this->display();
	}
	function edit_post(){
		if (IS_POST) {
			$tmlid = array(I("param.tmlid"));
			$startuph =I("post.startuph");
			$startupi =I("post.startupi");
			$shutdownh =I("post.shutdownh");
			$shutdowni =I("post.shutdowni");
			foreach(I("param.sound") as$k=>$sounds){
				$startup =date('H:i:s',strtotime($startuph[$k].':'.$startupi[$k].':00'));
				$shutdown =date('H:i:s',strtotime($shutdownh[$k].':'.$shutdowni[$k].':00'));
				$workc[$k]['bootTime'] =$startup;
				$workc[$k]['shutdownTime'] =$shutdown;
				$soundc[$k]['startTime'] =$startup;
				$soundc[$k]['endTime'] =$shutdown;
				$soundc[$k]['volume'] =$sounds;
			}
			$work = GameTable::workControl($tmlid,$workc);
			$sound = GameTable::soundControl($tmlid,$soundc);
			$bright = GameTable::brightControl($tmlid,I("post.bright"));
			if(is_array($work) && is_array($sound) && is_array($bright)){
				$result = $work['result_desc'].' - '.$sound['result_desc'].' - '.$bright['result_desc'];
			}else{
				$result = $work.' - '.$sound.' - '.$bright;
			}
			$this->success($result,U('Machine/index'));
		}
	}
	//开机
	function startup(){
		$tmlid = array(I("param.tmlid"));
		$result = GameTable::startup($tmlid);
		if(is_array($result)){
			$this->success($result['result_desc'],U('Machine/index'));
		}else{
			$this->error($result,U('Machine/index'));
		}
	}
	//关机
	function shutdown(){
		$tmlid = array(I("param.tmlid"));
		$result = GameTable::shutdown($tmlid);
		if(is_array($result)){
			$this->success($result['result_desc'],U('Machine/index'));
		}else{
			$this->error($result,U('Machine/index'));
		}
	}
	function update(){
        $search[] = '`devid` IS NOT NULL AND `status`=2';
        $search[] = array('dtype'=>array('in','AD,TA'));
		$devices=$this->device_model->where($search)->select();
		if($devices){
			$this->success("后台数据刷新中...",U('Machine/index'),0,'AJSON');
			$tmlsns = $tm = NULL;
			foreach($devices as $device){
				$tmlsns = GameTable::getTerminalsByTmlSNs(array($device['barcode']));
				if(is_array($tmlsns) && count($tmlsns) && $device['store_id']){
					$tm['devid'] = $device['devid'];
					$tm['tmlsn'] = $tmlsns[0]['tmlSN'];
					$tm['tmlid'] = $tmlsns[0]['id'];
					$tm['name'] = $tmlsns[0]['name'];
					if($tmlsns[0]['online']){
						$tm['online'] = 1;
					}else{
						$tm['online'] = 0;
					}
					$tm['tmldisk'] = $tmlsns[0]['tmlDisk'];
					$tm['tmltime'] = $tmlsns[0]['tmlTime'];
					$tm['version'] = $tmlsns[0]['version'];
					$tm['tmlipaddress'] = $tmlsns[0]['tmlIpAddress'];
					$tm['store_id'] = $device['store_id'];
					$gfind = $this->game_machine_model->where(array('devid'=>$device['devid'],'tmlsn'=>$tmlsns[0]['tmlSN'],'tmlid'=>$tmlsns[0]['id']))->find();
					if($gfind){
						$this->game_machine_model->where(array('devid'=>$device['devid'],'tmlsn'=>$tmlsns[0]['tmlSN'],'tmlid'=>$tmlsns[0]['id']))->save($tm);
					}else{
						$this->game_machine_model->add($tm);
					}
				}
			}
		}else{
    		$this->error("未查询到可用数据更新");
		}
	}
}